package com.weibo4terios.ui.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.weibo4terios.R;
import com.weibo4terios.dao.Status;
import com.weibo4terios.ui.imaCache.Anseylodar;
import com.weibo4terios.ui.util.WeiboViewHolder;


public class WeiboAdapter extends BaseAdapter {
	public List<Status> status;// 微博信息
	Context mContext;// 上下文
	Anseylodar ansylodar;
	Date nowDate;
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");// 格式化日期

	public WeiboAdapter(Context context, List<Status> allstatus) {
		status = allstatus;
		mContext = context;
		ansylodar = new Anseylodar();
		nowDate = new Date();
	}

	@Override
	public int getCount() {
		// 由于我们最后一项时更多 所以这里getcount+1
		return status.size();
	}

	@Override
	public Object getItem(int arg0) {
		return status.get(arg0);
	}

	// 请求更多的数据
	public void addmoreDate(List<Status> addmore) {
		if (status != null) {
			this.status.addAll(addmore);// 把新传得数据加到现在的list中
			this.notifyDataSetChanged();// 将数据追加到ListView中显示
		}
	}

	@Override
	public long getItemId(int index) {
		return status.get(index).getId();
	}

	@SuppressWarnings("static-access")
	@Override
	public View getView(int position, View conterView, ViewGroup arg2) {

		View statusView = null;
		if ((conterView != null) && (conterView.findViewById(R.id.contentPic) != null)) {
			statusView = conterView;
		} else {// 如果缓存中没有就动态加载一个itemview
			statusView = LayoutInflater.from(mContext).inflate(R.layout.weibo_item_view, null);
		}
		WeiboViewHolder vHolder = new WeiboViewHolder();
		// 查找原始微博UI
		vHolder.tvitemName = (TextView) statusView.findViewById(R.id.tvItemName);
		vHolder.tvContent = (TextView) statusView.findViewById(R.id.tvItemContent);
		vHolder.ivitemPorprait = (ImageView) statusView.findViewById(R.id.ivItemPortrait);
		vHolder.tvitemTimeData = (TextView) statusView.findViewById(R.id.tvItemDate);
		vHolder.tvRedirectNum = (TextView) statusView.findViewById(R.id.redirectNum);
		vHolder.tvCommentNum = (TextView) statusView.findViewById(R.id.commentNum);

		// 查找转发微博UI
		vHolder.subLayoutView = (LinearLayout) statusView.findViewById(R.id.subLayout);
		vHolder.tvitemSubcontent = (TextView) statusView.findViewById(R.id.tvItemSubContent);
		vHolder.contentPic = (ImageView) statusView.findViewById(R.id.contentPic);
		vHolder.subContenPic = (ImageView) statusView.findViewById(R.id.subContentPic);
		vHolder.tvSubRedirectNum = (TextView) statusView.findViewById(R.id.subRedirectNum);
		vHolder.tvSubCommentNum = (TextView) statusView.findViewById(R.id.subCommentNum);

		// 填充数据
		Status mstatus = status.get(position);
		if (mstatus.getUser().isVerified()) {// 判断是否为认证用户
			vHolder.ivitemV = (ImageView) statusView.findViewById(R.id.ivItemV);
			vHolder.ivitemV.setVisibility(View.VISIBLE);// 显示认证图标
		} else {
			vHolder.ivitemV = (ImageView) statusView.findViewById(R.id.ivItemV);
			vHolder.ivitemV.setVisibility(View.GONE);// 隐藏认证图标
		}
		// 填充发布该微博的用户名
		vHolder.tvitemName.setText(mstatus.getUser().getName());
		// 填充原始微博内容
		vHolder.tvContent.setText(mstatus.getText());
		// 填充该微博的发布时间
		long timeOffset = (nowDate.getTime() - mstatus.getCreatedAt().getTime()) / 60000;// 与当前时间相差多少
		if (timeOffset < 60) {
			vHolder.tvitemTimeData.setText(timeOffset + "分钟前");
		} else {
			vHolder.tvitemTimeData.setText(dateFormat.format(mstatus.getCreatedAt()).toString());
		}
		// 加载发布该微博的用户头像
		String usericon = mstatus.getUser().getProfileImageUrl();
		vHolder.ivitemPorprait.setImageResource(R.drawable.portrait);// 重置头像
		ansylodar.showimgAnsy(vHolder.ivitemPorprait, usericon);
		vHolder.tvRedirectNum.setText(String.valueOf(mstatus.getReposts_count()));
		vHolder.tvCommentNum.setText(String.valueOf(mstatus.getComments_count()));

		Status comentsStatus = status.get(position).getRetweeted_status();// 获取转发微博
		if (null != comentsStatus) {// 有转发
			if (comentsStatus.getDeleted() == 0) {// 转发微博未删除
				vHolder.contentPic.setVisibility(View.GONE);
				vHolder.subLayoutView.setVisibility(View.VISIBLE);// 设置转发微博布局可见
				vHolder.tvitemSubcontent.setText("@" + comentsStatus.getUser().getName() + ":" + comentsStatus.getText());
				String subconpic = comentsStatus.getThumbnail_pic();
				if (!TextUtils.isEmpty(subconpic)) {
					vHolder.subContenPic.setVisibility(View.VISIBLE);// 设置转发微博配图可见
					vHolder.subContenPic.setImageResource(R.drawable.preview_pic_loading);// 重置转发微博配图
					ansylodar.showimgAnsy(vHolder.subContenPic, subconpic);
				} else {
					vHolder.subContenPic.setVisibility(View.GONE);// 隐藏转发微博配图
				}
				vHolder.tvSubRedirectNum.setVisibility(View.VISIBLE);
				vHolder.tvSubCommentNum.setVisibility(View.VISIBLE);
				vHolder.tvSubRedirectNum.setText(String.valueOf(comentsStatus.getReposts_count()));
				vHolder.tvSubCommentNum.setText(String.valueOf(comentsStatus.getComments_count()));
			} else {// 被转发微博已经删除
				vHolder.contentPic.setVisibility(View.GONE);
				vHolder.subContenPic.setVisibility(View.GONE);// 设置转发微博的配图不可见
				vHolder.tvSubRedirectNum.setVisibility(View.VISIBLE);
				vHolder.tvSubCommentNum.setVisibility(View.VISIBLE);
				vHolder.subLayoutView.setVisibility(View.VISIBLE);// 设置转发微博的布局可见
				vHolder.tvitemSubcontent.setText(comentsStatus.getText());
			}
		} else {// 没有转发
			vHolder.subLayoutView.setVisibility(View.GONE);// 隐藏转发微博配图
			String pic_path = mstatus.getThumbnail_pic();
			if (!TextUtils.isEmpty(pic_path)) {
				vHolder.contentPic.setVisibility(View.VISIBLE);
				vHolder.contentPic.setImageResource(R.drawable.preview_pic_loading);// 重置原始微博配图
				ansylodar.showimgAnsy(vHolder.contentPic, pic_path);
			} else {
				vHolder.contentPic.setVisibility(View.GONE);
			}
		}
		return statusView;
	}

}
